class Solution {
    public:
        int is_existen[15][15] ={0};
        int maxgold = 0;
        int pathgold = 0;
        int getMaximumGold(vector<vector<int>>& grid) {
            int n = grid.size(),m=grid[0].size();
            for(int i = 0 ; i < n ; i++){
                for(int j =0 ; j < m ; j++){
                    pathgold +=grid[i][j];
                    is_existen[i][j] = 1;
                    if(grid[i][j] != 0){
                        dfs(grid,i,j);
                    }
                    pathgold =0;
                    is_existen[i][j] = 0;
                }
            }
            return maxgold;
        }
        int indexx[4] ={-1,1,0,0};
        int indexy[4]= {0,0,1,-1};
        //dfs的任务：从I,J位置开始探测，不包括I，j位置
        void dfs(vector<vector<int>>& grid,int i , int j){
            int n = grid.size();
            int m = grid[0].size();
            for(int k =0 ; k < 4 ;k++){
                int row = i+indexx[k];
                int col = j+indexy[k];
                if(row >= 0 && row < n&& col>=0 && col < m && grid[row][col] != 0&& is_existen[row][col] ==0){
                    is_existen[row][col] = 1;
                    pathgold += grid[row][col];
                    dfs(grid,row,col);
                    is_existen[row][col] = 0;
                    pathgold -= grid[row][col];
                }
            }
            maxgold = max(maxgold,pathgold);
            return;
        }
    };